package com.ql.mangersystem.controller;

import com.ql.mangersystem.common.HoneyLogs;
import com.ql.mangersystem.common.LogType;
import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.common.Result;
import com.ql.mangersystem.entity.dto.FilePageDTO;
import com.ql.mangersystem.entity.dto.FileSearchDTO;
import com.ql.mangersystem.entity.vo.RecycleVo;
import com.ql.mangersystem.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@RequestMapping("/recycle")
@RestController
@Slf4j
@Api("文件接口")
@CrossOrigin(value = "http://localhost:5173/")
public class RecycleBinController {
    @Resource
    private FileService fileService;

    @ApiOperation("文件分页查询")
    @PostMapping("/page")
    public Result page(@RequestBody FilePageDTO filePageDTO) {
        Page<RecycleVo> page = fileService.recycleByPage(filePageDTO);
        return Result.success(page);
    }
    @ApiOperation("根据id删除文件")
    @DeleteMapping("/delete/{id}")
    @HoneyLogs(operation = "回收站",type = LogType.DELETE)
    public Result Delete(@PathVariable("id") Integer id) {
        String name = fileService.selectName(id);
        String FILE_PATH = "D:\\path";
        String filePath = FILE_PATH + "/"+ name;
        boolean flag = false;
        //根据路径创建文件对象
        File file = new File(filePath);
        //路径是个文件且不为空时删除文件
        if(file.isFile() && file.exists()){
            flag = file.delete();
        }
        int result = fileService.delete(id);
        if (result<=0&& !flag){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
    @ApiOperation("根据id恢复")
    @PutMapping("/recover")
    @HoneyLogs(operation = "回收站",type = LogType.UPDATE)
    public Result recover(@RequestParam("id") Integer id) {
        int result = fileService.recover(id);
        if (result<=0){
            return Result.error("恢复失败");
        }
        return Result.success("恢复成功");
    }
    @ApiOperation("根据文件名模糊查询")
    @PostMapping("/searchByName")
    public Result searchByName(@RequestBody FileSearchDTO fileSearchDTO) {
        Page<RecycleVo> page = fileService.recycleByName(fileSearchDTO);
        if (page == null) {
            return Result.error("查询失败");
        }
        return Result.success(page);
    }
    @ApiOperation("批量恢复")
    @PutMapping("/recoverAll")
    @HoneyLogs(operation = "回收站",type = LogType.UPDATE)
    public Result recoverAll(@RequestParam List<Integer> ids) {
        int result = fileService.recoverAll(ids);
        if (result<=0){
            return Result.error("恢复失败");
        }
        return Result.success("恢复成功");
    }
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteAll")
    @HoneyLogs(operation = "回收站",type = LogType.BATCH_DELETE)
    public Result deleteAll(@RequestParam("ids") List<Integer> ids) {
        int result = fileService.deleteAll(ids);
        if (result<=0){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
}
